package com.edol.painting.painting;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;

public class MarkerBrush extends Brush {

	private Path mPath;
	private float mX;
	private float mY;
	private int mParticleCount;
	private float mX1 = 0;
	private float mY1 = 0;
	private float mX2 = 0;
	private float mY2 = 0;
	private float mX3 = 0;
	private float mY3 = 0;
	
	protected MarkerBrush(Context context) {
		super(context, BRUSH_MARKER);
		mPath = new Path();
		mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.BUTT);
        mPath.setFillType(Path.FillType.WINDING);
        BlurMaskFilter blurMaskFilter = new BlurMaskFilter(1, BlurMaskFilter.Blur.NORMAL); 
        mPaint.setMaskFilter(blurMaskFilter);
	}

	@Override
	public void setWidth(int width) {
		super.setWidth(width);
		mPaint.setStrokeWidth(mWidth);
	}

	@Override
	public void setColor(int color) {
		super.setColor(color);
		mPaint.setColor(mColor);
	}

	@Override
	protected void drawParticle(Canvas canvas, Particle particle, Rect bounds) {
		calLastBounds(mRoute, bounds);
		mLayerCanvas.save(Canvas.CLIP_SAVE_FLAG);
		mLayerCanvas.clipRect(bounds);
		mLayerCanvas.drawPath(mPath, mPaint);
		mLayerCanvas.restore();
	}

	private void updateLastParticle(float x1, float y1, float x2, float y2) {
		if (mParticleCount == 0) {
			mX1 = mX2 = mX;
			mY1 = mY2 = mY;
			mX3 = x2;
			mY3 = y2;
		} else {
			mX1 = mX3;
			mY1 = mY3;
			mX2 = x1;
			mY2 = y1;
			mX3 = x2;
			mY3 = y2;
		}
		mParticleCount += 2;
	}
	
	private void calLastBounds(Route route, Rect bounds) {
		int left = (int) (Math.min(mX1, Math.min(mX2, mX3)) - mWidth);
		int top = (int) (Math.min(mY1, Math.min(mY2, mY3)) - mWidth);
		int right = (int) (Math.max(mX1, Math.max(mX2, mX3)) + mWidth);
		int bottom = (int) (Math.max(mY1, Math.max(mY2, mY3)) + mWidth);
		bounds.union(left, top, right, bottom);
	}
	
	@Override
	public void beginRoute(Canvas canvas, Particle particle, Rect bounds) {
		mPath.reset();
		mParticleCount = 0;
		mPath.moveTo(particle.x, particle.y);
		mX = particle.x;
		mY = particle.y;
	}

	@Override
	public void drawRoute(Canvas canvas, Particle particle, Rect bounds) {
		if (canvas != null && particle != null && bounds != null && mLayer != null && mLayerCanvas != null) {
			float midX =  (particle.x + mX) / 2;
			float midY = (particle.y + mY) / 2;
			updateLastParticle(mX, mY, midX, midY);
			mPath.quadTo(mX, mY, midX, midY);
			mX = particle.x;
			mY = particle.y;
			mBounds.setEmpty();
			drawParticle(canvas, particle, mBounds);
			if (!mBounds.isEmpty()) {
				bounds.union(mBounds);
			}
		}
	}

	@Override
	public void endRoute(Canvas canvas, Particle particle, Rect bounds) {
		if (canvas != null && particle != null && bounds != null && mLayer != null && mLayerCanvas != null) {
			float midX =  (particle.x + mX) / 2;
			float midY = (particle.y + mY) / 2;
			updateLastParticle(mX, mY, midX, midY);
			mPath.lineTo(midX, midY);
			mBounds.setEmpty();
			drawParticle(canvas, particle, mBounds);
			if (!mBounds.isEmpty()) {
				bounds.union(mBounds);
			}
		}
	}
}
